package net.zhengxinyun.performance.controller.incentive;


import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.IncentiveEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.IncentiveFilter;
import net.zhengxinyun.performance.service.incentive.IncentiveService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: hyp
 * @Date: 2018/11/4 15:24
 */
@RestController
@RequestMapping("/incentive")
public class IncentiveController {

    private IncentiveService incentiveService;
    private TokenService tokenService;

    @Autowired
    public IncentiveController(IncentiveService incentiveService, TokenService tokenService) {
        this.incentiveService = incentiveService;
        this.tokenService = tokenService;
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody IncentiveParam incentiveParam) {
        if (tokenService.compareToken(incentiveParam.tokenEntity.getValue())) {
            String str = incentiveService.delete(incentiveParam.incentiveEntity);
            if (!"删除成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @RequestMapping("/update")
    public Result<String> update(@RequestBody IncentiveParam incentiveParam) {
        if (tokenService.compareToken(incentiveParam.tokenEntity.getValue())) {
            String str = incentiveService.update(incentiveParam.incentiveEntity);
            if (!"修改成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody IncentiveParam incentiveParam) {
        if (tokenService.compareToken(incentiveParam.tokenEntity.getValue())) {
            String str = incentiveService.insert(incentiveParam.incentiveEntity);
            if (!"添加成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/query")
    public Result<HashMap<String, Object>> query(@RequestBody IncentiveParam incentiveParam) {
        if (tokenService.compareToken(incentiveParam.tokenEntity.getValue())) {
            HashMap<String, Object> map = new HashMap<>();
            List<Map<String, Object>> list = incentiveService.query(incentiveParam.incentiveFilter);
            if (list.size() == 0) {
                incentiveParam.incentiveFilter.setPageNumber(incentiveParam.incentiveFilter.getPageNumber() - 1);
                list = incentiveService.query(incentiveParam.incentiveFilter);
            }
            Double total = Util.intGetTotalPage(incentiveService.queryForCount(incentiveParam.incentiveFilter), incentiveParam.incentiveFilter.getPageSize());
            map.put("list", list);
            map.put("total", total);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/pass")
    public Result<String> pass(@RequestBody IncentiveParam incentiveParam) {
        if (tokenService.compareToken(incentiveParam.tokenEntity.getValue())) {
            String str = incentiveService.pass(incentiveParam.incentiveEntity);
            if (!"审核成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/noPass")
    public Result<String> noPass(@RequestBody IncentiveParam incentiveParam) {
        if (tokenService.compareToken(incentiveParam.tokenEntity.getValue())) {
            String str = incentiveService.noPass(incentiveParam.incentiveEntity);
            if (!"回退成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/audit")
    public Result<HashMap<String,Object>> queryForAudit(@RequestBody IncentiveParam incentiveParam) {
        if (tokenService.compareToken(incentiveParam.tokenEntity.getValue())) {
            HashMap<String, Object> map = new HashMap<>();
            List<Map<String, Object>> list = incentiveService.queryForAudit(incentiveParam.incentiveFilter);
            if (list.size() == 0) {
                incentiveParam.incentiveFilter.setPageNumber(incentiveParam.incentiveFilter.getPageNumber() - 1);
                list = incentiveService.query(incentiveParam.incentiveFilter);
            }
            Double total = Util.intGetTotalPage(incentiveService.queryForAuditCount(incentiveParam.incentiveFilter), incentiveParam.incentiveFilter.getPageSize());
            map.put("list", list);
            map.put("total", total);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/pointType")
    public Result<HashMap<String,Object>> pointType(@RequestBody IncentiveParam incentiveParam){
        List<IncentiveFilter> pointList;
        if (StringUtils.isNotBlank(incentiveParam.incentiveFilter.getDateTime())){
            pointList = incentiveService.queryGroupType(incentiveParam.incentiveFilter.getDateTime());
        }else {
            pointList = incentiveService.queryGroupType("");
        }
        List<IncentiveFilter> monthList;
        if (StringUtils.isNotBlank(incentiveParam.incentiveFilter.getDateTime())){
            monthList = incentiveService.queryGroupMonth(incentiveParam.incentiveFilter.getDateTime());
        }else {
            monthList = incentiveService.queryGroupMonth("");
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("pointType",pointList);
        map.put("monthType",monthList);
        return ResultUtils.getSuccessResults(map);
    }

   /* @PostMapping("/monthType")
    public Result<ArrayList<IncentiveFilter>> monthType(@RequestBody IncentiveParam incentiveParam){
        List<IncentiveFilter> list;
        if (StringUtils.isNotBlank(incentiveParam.incentiveFilter.getDateTime())){
            list = incentiveService.queryGroupMonth(incentiveParam.incentiveFilter.getDateTime());
        }else {
            list = incentiveService.queryGroupMonth("");
        }
        return ResultUtils.getSuccessResultList(list);
    }*/
}

class IncentiveParam {
    public TokenEntity tokenEntity;
    public IncentiveFilter incentiveFilter;
    public IncentiveEntity incentiveEntity;
}
